package com.llgz.hotelmanageapi.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llgz.hotelmanageapi.common.CommonResult;
import com.llgz.hotelmanageapi.pojo.DO.CheckinInfo;
import com.llgz.hotelmanageapi.service.impl.CheckinInfoServiceImpl;
import com.llgz.hotelmanageapi.service.impl.ConsumptionInfoServiceImpl;
import com.llgz.hotelmanageapi.service.impl.RoomReserveInfoServiceImpl;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @Author: Himit_ZH
 * @Date: 2021/7/25 12:24
 * @Description:
 */
@RequestMapping("/reportForm")
@Controller
// @RequiresRoles(value = {"root", "admin", "manager"}, logical = Logical.OR)
public class ReportFormController {

    @Autowired
    private ConsumptionInfoServiceImpl consumptionInfoService;

    @Autowired
    private CheckinInfoServiceImpl checkinInfoService;

    @Autowired
    private RoomReserveInfoServiceImpl roomReserveInfoService;


    /**
     * @MethodName consumption
     * @param commodityType 商品类别
     * @param roomId 客房编号
     * @Description 需求3.3.14 消费管理
     * @Return
     * @Since 2021/7/25
     */
    @GetMapping("/consumption")
    public void consumption(@RequestParam(value = "commodityType", required = false) Integer commodityType,
                            @RequestParam(value = "roomId", required = false) Integer roomId,
                            HttpServletResponse response) throws IOException {

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("contest_" + UUID.randomUUID().toString() + "_rank", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Content-Type", "application/xlsx");
        // 根据商品编号查询消费记录表
        if (commodityType != null) {
            EasyExcel.write(response.getOutputStream())
                    .head(consumptionInfoService.getConsumptionExcelHead())
                    .sheet("consumption")
                    .doWrite(consumptionInfoService.getConsumptionDataByCommodityType(commodityType));
            return;
        }

        if (roomId != null) {
            EasyExcel.write(response.getOutputStream())
                    .head(consumptionInfoService.getConsumptionExcelHead())
                    .sheet("consumption")
                    .doWrite(consumptionInfoService.getConsumptionDataByRoomId(roomId));
        }

    }


    /**
     * @MethodName reserve
     * @param beginDate 开始日期
     * @param endDate 结束日期
     * @param roomId 房间id
     * @Description 需求3.3.15 报表管理  1 预定客人报表 和 3 当日预定客人报表
     * @Return
     * @Since 2021/7/25
     */
    @GetMapping("/reserve")
    // @ResponseBody
    public void reserve(@RequestParam(value = "beginDate", required = false) String beginDate,
                                @RequestParam(value = "endDate", required = false) String endDate,
                                @RequestParam(value = "roomId", required = false) Integer roomId,
                                HttpServletResponse response) throws IOException, ParseException {

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("contest_" + UUID.randomUUID().toString() + "_rank", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Content-Type", "application/xlsx");

        String startDayTime = null;
        String endDayTime = null;
        // 默认为当日
        if (endDate == null && beginDate == null) {

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            Date dzDateObj = new Date();
            startDayTime = sdf2.format(dzDateObj) + " 00:00:00"; // 2021-07-25 00:00:00

            // 当日加一天
            Calendar cal = Calendar.getInstance();
            cal.setTime(dzDateObj);
            cal.add(Calendar.DAY_OF_YEAR, 1);
            Date addDate = cal.getTime();
            endDayTime = sdf2.format(addDate) + " 00:00:00";// 2021-07-26 00:00:00
        } else {

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            Date dzDateObj = sdf1.parse(beginDate);
            startDayTime = sdf2.format(dzDateObj) + " 00:00:00"; // 2021-07-25 00:00:00

            // 结束日期加一天
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf1.parse(endDate));
            cal.add(Calendar.DAY_OF_YEAR, 1);
            Date addDate = cal.getTime();
            endDayTime = sdf2.format(addDate) + " 00:00:00";// 2021-07-26 00:00:00
        }

        EasyExcel.write(response.getOutputStream())
                .head(roomReserveInfoService.getReserveExcelHead())
                .sheet("checkIn")
                .doWrite(roomReserveInfoService.getReserveDataByDateOrRoomId(startDayTime, endDayTime, roomId));


        // return CommonResult.successResponse(roomReserveInfoService.getReserveDataByDateOrRoomId(startDayTime, endDayTime, roomId));
    }


    /**
     * @MethodName checkIn
     * @param beginDate 开始日期
     * @param endDate 结束日期
     * @param roomId 房间id
     * @Description 需求3.3.15 报表管理  2 入住客人报表 和 4 当日入住客人报表
     * @Return
     * @Since 2021/7/25
     */
    @GetMapping("/check-in")
    public void checkIn(@RequestParam(value = "beginDate", required = false) String beginDate,
                        @RequestParam(value = "endDate", required = false) String endDate,
                        @RequestParam(value = "roomId", required = false) Integer roomId,
                        HttpServletResponse response) throws IOException, ParseException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("contest_" + UUID.randomUUID().toString() + "_rank", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Content-Type", "application/xlsx");

        String startDayTime = null;
        String endDayTime = null;

        // 默认为当日
        if (endDate == null && beginDate == null) {

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            Date dzDateObj = new Date();
            startDayTime = sdf2.format(dzDateObj) + " 00:00:00"; // 2021-07-25 00:00:00

            // 当日加一天
            Calendar cal = Calendar.getInstance();
            cal.setTime(dzDateObj);
            cal.add(Calendar.DAY_OF_YEAR, 1);
            Date addDate = cal.getTime();
            endDayTime = sdf2.format(addDate) + " 00:00:00";// 2021-07-26 00:00:00
        } else {

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            Date dzDateObj = sdf1.parse(beginDate);
            startDayTime = sdf2.format(dzDateObj) + " 00:00:00"; // 2021-07-25 00:00:00

            // 结束日期加一天
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf1.parse(endDate));
            cal.add(Calendar.DAY_OF_YEAR, 1);
            Date addDate = cal.getTime();
            endDayTime = sdf2.format(addDate) + " 00:00:00";// 2021-07-26 00:00:00
        }

        EasyExcel.write(response.getOutputStream())
                .head(checkinInfoService.getCheckInExcelHead())
                .sheet("checkIn")
                .doWrite(checkinInfoService.getCheckInDataByDateAndRoomId(startDayTime, endDayTime, roomId));

    }


}